package org.ace.biz.user.impl;

import java.util.Collection;

import javax.annotation.Resource;

import org.ace.basicdata.IBasicDataCentral;
import org.ace.basicdata.model.ItemBasicData;
import org.ace.biz.user.IPlayerBiz;
import org.ace.biz.user.IPlayerItemBiz;
import org.ace.cache.user.IPlayerItemCache;
import org.ace.constans.ItemConstans;
import org.ace.constans.PlayerEventConstans;
import org.ace.dao.model.PlayerItem;
import org.ace.event.AceEventUtils;
import org.ace.event.model.PlayerEvent;
import org.springframework.stereotype.Service;
@Service("playerItemBiz")
public class PlayerItemBiz implements IPlayerItemBiz {

	@Resource(name="playerItemCache")
	private IPlayerItemCache playerItemCache;
	
	@Resource(name="itemBasicDataCenteral")
	private IBasicDataCentral<ItemBasicData> itemBasicDataCenteral;
	
	@Resource(name="playerBiz")
	private IPlayerBiz playerBiz;
	
	@Resource(name="eventUtils")
	private AceEventUtils eventUtils;
	
	@Override
	public boolean add(Integer userId, Integer gdCode, Integer count) {
		if(count<0) return false;
		//非法数据
		if(!itemBasicDataCenteral.has(gdCode)) return false;
		
		if(!playerItemCache.hasNullGrid(userId, gdCode)) return false;
		
		playerItemCache.add(userId, gdCode, count);
		return true;
	}

	@Override
	public boolean delete(Integer userId, Integer gdCode, Integer count) {
		if(count<0) return false;
		//有的话进行删除操作
		if(playerItemCache.has(userId, gdCode, count))
		{
			playerItemCache.spend(userId, gdCode, count);
			return true;
		}
		return false;
	}

	/*使用道具*/
	@Override
	public boolean spend(Integer userId, Integer gdCode, Integer count) {
		if(count<0) return false;
		if(!playerItemCache.has(userId, gdCode, count)) return false;
		//itemBasicDataCentral 管理Item 里面所有的条目
		ItemBasicData data=itemBasicDataCenteral.get(gdCode);
		PlayerEvent event;
		switch(data.getAttribute())
		{ //道具影响HP 则调用hp变更接口
		 case ItemConstans.HP:
			 playerBiz.hpChange(userId, data.getValue());
			 //属性变更事件,并发放
			  event=new PlayerEvent(this, userId, playerBiz.get(userId).getHp(), PlayerEventConstans.HP_CHANGE);
			 eventUtils.DispatchEvent(event);
			 break;
		 case ItemConstans.EX:
			 if(playerBiz.addExp(userId, data.getValue()))
			 {
				 event=new PlayerEvent(this,userId,playerBiz.get(userId).getLv(),PlayerEventConstans.LEVEL_UP);
				 eventUtils.DispatchEvent(event);
			 }
			 break;
		}
		
		playerItemCache.spend(userId, gdCode, count);
		return true;
	}

	@Override
	public Collection<PlayerItem> getList(Integer userId) {
		
		return playerItemCache.getList(userId);
	}

}
